package com.etoak.controller;

import com.etoak.common.core.vo.ResultVO;
import com.etoak.entity.Cart;
import com.etoak.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    CartService cartService;

    /**
     * 添加、修改 post /cart
     */
    @PostMapping
    public ResultVO<Object> saveOrUpdate(@Validated @RequestBody Cart cart) {
        cartService.saveOrUpdate(cart);
        return ResultVO.success();
    }

    /**
     * 查询购物车菜品列表 get /cart/list
     */
    @GetMapping("/list")
    public ResultVO<List<Cart>> getList() {
        List<Cart> list = cartService.getList();
        return ResultVO.success(list);
    }

    /**
     * 删除购物车中的菜品 delete /cart/dishes/{id}
     */
    @DeleteMapping("/dishes/{id}")
    public ResultVO<Object> deleteDishes(@PathVariable String id) {
        cartService.deleteDishes(id);
        return ResultVO.success();
    }

    /**
     * 清空购物车 /cart
     */
    @DeleteMapping
    public ResultVO<Object> clear() {
        cartService.clear();
        return ResultVO.success();
    }

}
